package com.trs.test.aidl;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button bindButton;
    Button inButton;
    Button outButton;
    Button inoutButton;
    Button onewayButton;
    private static final String TAG = MainActivity.class.getSimpleName();

    ITestAidlInterface iTestAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initCtrl();
    }

    void initCtrl() {
        bindButton = (Button) findViewById(R.id.btn_bind);
        bindButton.setOnClickListener(this);
        inButton = (Button) findViewById(R.id.btn_in);
        inButton.setOnClickListener(this);
        outButton = (Button) findViewById(R.id.btn_out);
        outButton.setOnClickListener(this);
        inoutButton = (Button) findViewById(R.id.btn_inout);
        inoutButton.setOnClickListener(this);
        onewayButton = (Button) findViewById(R.id.btn_oneway);
        onewayButton.setOnClickListener(this);
    }
    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iTestAidlInterface = ITestAidlInterface.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iTestAidlInterface = null;
        }
    };

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_bind:
                Intent intent = new Intent();
                intent.setClass(this, TestAIDLService.class);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
                break;
            case R.id.btn_in:
                Person inPerson = new Person();
                inPerson.name = "in";
                inPerson.age = 0;
                try {
                    Log.i(TAG, inPerson.toString());
                    iTestAidlInterface.testIn(inPerson);
                    Log.i(TAG, inPerson.name);
                    Log.i(TAG, inPerson.age + "");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_out:
                Person outPerson = new Person();
                try {
                    Log.i(TAG, outPerson.toString());
                    iTestAidlInterface.testOut(outPerson);
                    Log.i(TAG, outPerson.name);
                    Log.i(TAG, outPerson.age + "");

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_inout:
                Person inoutPerson = new Person();
                inoutPerson.name = "inout";
                inoutPerson.age = 0;
                try {
                    Log.i(TAG, inoutPerson.toString());
                    iTestAidlInterface.testInOut(inoutPerson);
                    Log.i(TAG, inoutPerson.name);
                    Log.i(TAG, inoutPerson.age + "");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_oneway:
                try {
                    iTestAidlInterface.testOneway();
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                Log.i(TAG, "invoke return");
                break;
        }
    }
}
